package plugin.conditions;

import java.io.File;
import java.util.List;

import gui.ActPreAndPostCondWindow;

import models.action.Action;
import models.asl.Graph;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import reader.ActionReader;
import reader.Transformation;


public class ConditionEditor implements IObjectActionDelegate {

    private String filePath;
    public static String projectPath;
    public static String fileName;
    /** The act pre and post cond window. */
    private ActPreAndPostCondWindow actPreAndPostCondWindow;
    
    @Override
    public void run(IAction arg0) {
        System.out.println("Project Path:" + projectPath);
        System.out.println("File Path:" + filePath);
        actPreAndPostCondWindow = new ActPreAndPostCondWindow();
        Transformation transformation = new Transformation(filePath);
        List<Graph> graphs;
        // check to see if there is previous cached actions pre & post conditions
        List<Action> preActions = null;
        try {
            ActionReader ar = new ActionReader();
            preActions = ar.readActions(projectPath + File.separator
                    + "temp" + File.separator + fileName + ".action");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        // try to find previous saved pre and post conditions

        try {
            graphs = transformation.generate();
            // for (Graph g : graphs) {
            List<String> actions = graphs.get(0).getAgentActions();
            for (String action : actions) {
                String r[] = new String[] { action, "", "" };
                if (preActions != null && !preActions.isEmpty()) {
                    for (Action preAction : preActions) {
                        if (preAction.getName().equalsIgnoreCase(action)) {
                            r[1] = parseConditions(preAction.getPreCondition());
                            r[2] = parseConditions(preAction.getPostCondition());
                        }
                    }
                }

                actPreAndPostCondWindow.getDefaultTableModel().addRow(r);
            }
            // }

        } catch (Exception e) {
            e.printStackTrace();
        }

        actPreAndPostCondWindow.setVisible(true);

    }

    @Override
    public void selectionChanged(IAction arg0, ISelection arg1) {
        if(arg1 instanceof IStructuredSelection) {
            Object obj = ((IStructuredSelection) arg1).getFirstElement();
            IFile file = (IFile) Platform.getAdapterManager().getAdapter(obj,
                    IFile.class);
            if (file == null) {
                if (obj instanceof IAdaptable) {
                    file = (IFile) ((IAdaptable) obj).getAdapter(IFile.class);
                }
            }
            if(file != null) {
                filePath = file.getRawLocation().toOSString();
                fileName = file.getName();
                projectPath = file.getProject().getLocation().toOSString();
            }

        }
        
    }

    @Override
    public void setActivePart(IAction arg0, IWorkbenchPart arg1) {
        // TODO Auto-generated method stub
        
    }
    
    /**
     * Parses the conditions.
     * 
     * @param conditions
     *            the conditions
     * @return the string
     */
    private String parseConditions(List<String> conditions) {
        String condString = "";
        if (conditions != null) {
            int count = 0;
            for (String condition : conditions) {
                if (count++ != 0) {
                    condString += ";";
                }
                condString += condition;
            }
        }
        return condString;
    }

}
